
package com.kuang.murule;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.*;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;


public class KuangRandomRule extends AbstractLoadBalancerRule {

    //每个服务访问五次,换下一个服务

    /**
     * total= 0 默认等于0 如果等于五我们指向下一个服务节点
     * index = 0 默认0,如果total==5 index+1  如果三台机器大于3 total志伟
     *
     * @param lb
     * @param key
     * @return
     */
    private int total = 0; //总共被调用的次数
    private int currentIndex = 0; //当前是谁在提供服务


    //    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")
    public Server choose(ILoadBalancer lb, Object key) {
        if (lb == null) {
            return null;
        }
        Server server = null;

        while (server == null) {
            if (Thread.interrupted()) {
                return null;
            }

            List<Server> upList = lb.getReachableServers();//获得活着的服务
            List<Server> allList = lb.getAllServers();//获得全部的服务

            int serverCount = allList.size();
            if (serverCount == 0) {
                return null;
            }


/*            int index = chooseRandomInt(serverCount); //生成区间随机数
            //通过活着的服务中随机获取一个
            server = upList.get(index);*/
//=============================================================================

            if (total < 5) {
                server = upList.get(currentIndex);
                total++;
            } else {
                total = 0;

                currentIndex++;
                if (currentIndex > upList.size()) {
                    currentIndex = 0;
                }

                server = upList.get(currentIndex);
                //从活着的服务获取
            }
//=============================================================================
            if (server == null) {
                Thread.yield();
                continue;
            }
            //如果线程存活
            if (server.isAlive()) {
                server.isAlive();
                return (server);
            }
            server = null;
            Thread.yield();
        }
        return server;

    }

    protected int chooseRandomInt(int serverCount) {
        return ThreadLocalRandom.current().nextInt(serverCount);
    }

    @Override
    public Server choose(Object key) {
        return choose(getLoadBalancer(), key);
    }

    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {
        // TODO Auto-generated method stub

    }
}
